//公共图表
var colors = ["#55ce63", "#009efb", "#f62d51", "#26c6da ", "#ffbc34", "#7460ee", "#DA70D6", "#2f3d4a", "#90a4ae", "#55ce63", "#0066CC", "#CC3399", "#9999FF", "#5F9EA0", "#008B8B"];
var grid = {left: '3%', right: '4%', top: '10%', bottom: '2%', containLabel: true},
    toolbox = {
        show: true,
        feature: {magicType: {show: true, type: ['line', 'bar']}, restore: {show: true}, saveAsImage: {show: true}}
    },
    color = colors,
    calculable = true,
    yAxis = {
        axisLine: {
            show: false,
            lineStyle: {
                color: '#99abb4'
            }
        },
        splitLine: {lineStyle: {type: 'dashed'}},//虚线
        axisTick: {
            show: false
        },
        type: 'value',
        axisTick: {
            length: '5'
        }
    }
var echartsUtil = {
    line: function (ele, url, map) {//折线图
        var dom = document.getElementById(ele), myChart = echarts.init(dom);
        myChart.showLoading();//显示正在加载
        $.post(url, map, function (data) {
            myChart.hideLoading();//结束正在加载
            if (data.state) {
                noData(data);
                myChart.dispose();//清空图表
                return;
            }
            var arrName = new Array(), num = 0, series = [], unit = [], times = [];
            $.each(data, function (i, v) {
                $.each(v, function (j, d) {
                    times.push(d.time);
                })
            })
            times = Array.from(new Set(times));//数组排序
            $.each(data, function (i, v) {
                arrName[num] = v[0].name;
                num++;
                unit.push({unit: v[0].unit, name: v[0].name});
                var option = {
                    /*title: {
                        text: title
                    },*/
                    tooltip: {
                        trigger: 'axis', formatter: function (params) {
                            return fun_tooltip(params, unit)
                        }
                    },
                    legend: {
                        data: arrName
                    },
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            inverse: false,//从左边开始
                            data: times.sort()
                        }
                    ],
                    series: functionName(v, series, times)
                };
                if (option && typeof option === "object") {
                    option.grid = grid;
                    option.toolbox = toolbox;
                    option.color = color;
                    option.calculable = calculable;
                    option.yAxis = yAxis;
                    myChart.setOption(option, true);
                }
            })
        })
        //实时改变大小---单个图
        /*window.onresize = function () {
            myChart.resize();
        }*/
        //多个图
        window.addEventListener("resize", function () {
            myChart.resize();
        })
    },
    realLine: function (ele, data) {//实时折线图
        var myChart;
        /*if (myChart != null && myChart != "" && myChart != undefined) {
            myChart.dispose();//销毁
            console.debug("销毁了dom")
        }*/
        var dom = document.getElementById(ele);
        myChart = echarts.init(dom);
        if (data.state) {
            noData(data);
            return;
        }
        var arrName = new Array(), num = 0, series = [], unit = [], times = [];
        $.each(data, function (i, v) {
            $.each(v, function (j, d) {
                times.push(d.time);
            })
        })
        times = Array.from(new Set(times));//数组排序
        $.each(data, function (i, v) {
            arrName[num] = v[0].name;
            num++;
            unit.push({unit: v[0].unit, name: v[0].name});
            var option = {
                /*title: {
                    text: title
                },*/
                tooltip: {
                    trigger: 'axis', formatter: function (params) {
                        return fun_tooltip(params, unit)
                    }
                },
                legend: {
                    data: arrName
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        inverse: false,//从左边开始
                        data: times.sort()
                    }
                ],
                series: functionName(v, series, times)
            };
            if (option && typeof option === "object") {
                option.grid = grid;
                option.toolbox = toolbox;
                option.color = color;
                option.calculable = calculable;
                option.yAxis = yAxis;
                myChart.setOption(option, false);
            }
        })
        //实时改变大小
        window.addEventListener("resize", function () {
            myChart.resize();
        })
    },
    bar: function (ele, data, title) {//柱状图
        var dom = document.getElementById(ele), myChart = echarts.init(dom),
            arrName = new Array(), num = 0, series = [], unit = [], times = [];
        $.each(data, function (i, v) {
            $.each(v, function (j, d) {
                times.push(d.time.substring(10));
            })
        })
        times = Array.from(new Set(times));//数组排序
        $.each(data, function (i, v) {
            arrName[num] = v[0].name;
            num++;
            unit.push({unit: v[0].unit, name: v[0].name});
            var option = {
                    title: {
                        text: title,
                        textStyle: {
                            color: '#99abb4'
                        }
                    },
                    tooltip: {
                        trigger: 'axis', formatter: function (params) {
                            return fun_tooltip(params, unit)
                        }
                    },
                    legend: {
                        show: false,
                        data: arrName
                    },
                    textStyle: {
                        color: '#99abb4'
                    },
                    xAxis:
                        {
                            axisLine: {
                                show: true,
                                lineStyle: {
                                    color: '#99abb4'
                                }
                            },
                            type: 'category',
                            boundaryGap: true,
                            splitLine: {lineStyle: {type: 'dashed'}},
                            inverse: false,//从左边开始
                            data: times.sort()
                        }
                    ,
                    series:
                        fun_series(v, series, times, 'bar')
                }
            ;
            if (option && typeof option === "object") {
                option.grid = grid;
                option.toolbox = toolbox;
                option.toolbox.show = false;
                option.color = color;
                option.calculable = calculable;
                option.yAxis = yAxis;
                myChart.setOption(option, true);
            }
        })
        //实时改变大小
        /* window.onresize = function () {
             myChart.resize();
         }*/
        window.addEventListener("resize", function () {
            myChart.resize();
        });
    },
    miniBar: function (ele, data) {//首页的缩小版图
        var dom = document.getElementById(ele), myChart = echarts.init(dom),
            series = [], unit = [], times = [];
        if (data.length == 0) {
            $(dom).html("<div style='text-align: center;'>暂无数据</div>")
        }
        $.each(data, function (i, v) {
            times.push(v.time);
        })
        times = Array.from(new Set(times));//数组排序
        var option = {
                tooltip: {
                    show: true,
                    trigger: 'axis',
                    axisPointer: {
                        type: 'none'//去掉鼠标指示线
                    }
                },
                textStyle: {
                    color: '#99abb4'
                },
                xAxis:
                    {
                        axisLine: {
                            show: false,
                            lineStyle: {
                                color: '#99abb4'
                            }
                        },
                        type: 'category',
                        boundaryGap: false,
                        splitLine: {lineStyle: {type: 'dashed'}},
                        inverse: false,
                        show: false,
                        data: times.sort()
                    }
                ,
                yAxis: {
                    show: false
                },
                series:
                    fun_series2(data, series, times, 'line')
            }
        ;
        if (option && typeof option === "object") {
            option.grid = grid;
            option.toolbox = toolbox;
            option.toolbox.show = false;
            option.color = color;
            option.calculable = calculable;
            myChart.setOption(option, true);
        }
        //实时改变大小
        window.onresize = function () {
            myChart.resize();
        }
    },
    pie: function (ele, url, map) {
        var dom = document.getElementById(ele), myChart = echarts.init(dom);
        myChart.showLoading();//显示正在加载
        $.post(url, map, function (data) {
            myChart.hideLoading();//结束正在加载
            if (data.state || data.a == null) {
                noData(data);
                myChart.dispose();//清空图表
                return;
            }
            var option = {
                title: {
                    text: '数据情况占比',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['正常', '预警', '报警']
                },
                color: ["green", "orange", "red"],
                series: [
                    {
                        name: '数据情况占比',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        data: [
                            {value: data.a, name: '正常'},
                            {value: data.b, name: '预警'},
                            {value: data.c, name: '报警'}
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            myChart.setOption(option, true);
        })
        //实时改变大小
        window.onresize = function () {
            myChart.resize();
        }
    }
}

//echartsUtil.line("main", "/ctsData/chartAllGas", {equiminetId: $("#equiminetInfo").val()});


function functionName(data2, series, times) {
    var arr = [],
        names;
    $.each(data2, function (i, ivalue) {
        $.each(times, function (j, d) {
            if (ivalue.time == d) {
                arr[j] = ivalue.value;
            } else {
                //arr[i] = "";
            }
            if (i == 0) {
                names = ivalue.name
            }
        });
    });
    series.push({
        name: names,
        type: 'line',
        data: arr,
        smooth: true,
        markPoint: {
            data: [
                {type: 'max', name: '最大值'},
                {type: 'min', name: '最小值'}
            ]
        }
        ,
        markLine: {
            data: [
                {type: 'average', name: '平均值'}
            ]
        }
    })
    ;
    return series;
}

function fun_series(data2, series, times, type) {
    var arr = [],
        names;
    $.each(data2, function (i, ivalue) {
        $.each(times, function (j, d) {
            if (ivalue.time.substring(10) == d) {
                arr[j] = ivalue.value;
            }
            if (i == 0) {
                names = ivalue.name
            }
        });
    });
    series.push({
        name: names,
        type: type,
        data: arr
    });
    return series;
}

//隐藏圆点
function fun_series2(data, series, times, type) {
    var arr = [];
    $.each(data, function (j, d) {
        arr[j] = d.value;
    });
    series.push({
        type: type,
        smooth: true,
        symbol: 'none',
        data: arr
    });
    return series;
}

function fun_tooltip(params, unit) {
    var p = params[0].name + "<br>";
    $.each(params, function (i2, v2) {
        if (v2.data != undefined) {
            $.each(unit, function (i3, v3) {
                if (v2.seriesName == v3.name) {
                    p += '<i class="fa fa-circle font-10 m-r-10" style="color: ' + v2.color + '"></i>' + v2.seriesName + ":" + v2.data + '&nbsp;' + v3.unit + "<br>"
                }
            })
        }
    });
    return p
}

function noData(i) {
    showMsg(1, i.msg || "无数据！");
}